﻿//Szu
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace CharacterLibrary
{
    /// <summary>
    /// Z danej klasy dziedziczą wszystkie obiekty, które znajdą się na planszy.
    /// </summary>
    public abstract class Character
    {
        
        public enum DirectionType { down = 0, //z++
                                    right,    //x++
                                    up,       //z--
                                    left };   //x--
        protected Vector3 position;
        protected Vector3 bornPlace;
        protected Model model;
        protected float speed = 1.0f;

        protected DirectionType direction = DirectionType.down;
                                               //0 - Z direction
                                               //1 - X direction
                                               //2 - -Z direction
                                               //3 - -X direction

        public Vector3 Pos
        {
            set
            {
                position = value;
            }
            get
            {
                return position;
            }
        }

        public Vector3 BornPlace
        {
            get
            {
                return bornPlace;
            }
        }
      
        public DirectionType Direction
        {
            get
            {
                return direction;
            }
            set
            {
                direction = value;
            }
        }
        public void resetPosition()
        {
            position = bornPlace;
        }
        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="rows"></param>
        /// <param name="columns"></param>
        public Character(GraphicsDeviceManager graphics, int rows, int columns)
        {
            position = new Vector3();
            bornPlace = new Vector3();            
        }
        /// <summary>
        /// Rysowanie obiektu na planszy
        /// </summary>
        public virtual void Draw()
        {

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = Matrix.CreateRotationY( ((float)Math.PI)*(int)direction / 2 +(float)(Math.PI) )*Matrix.CreateTranslation(position);
                  /*
                  switch(direction)
                  {
                    case DirectionType.down: objectDirection = 
                  }
                  objectDirection = direction
                  */
                    effect.World = Matrix.CreateRotationY(((float)Math.PI) * (int)direction/*(int)direction*/ / 2 + (float)(Math.PI)) * Matrix.CreateTranslation(position);
                    effect.View = Global.Vision.view;
                    effect.Projection = Global.Vision.projection;
                    effect.EnableDefaultLighting();
                }
                mesh.Draw();
            }
        }
        /// <summary>
        /// Wykrywanie kolizji pomiędzy dwoma obiektami.
        /// </summary>
        /// <param name="character"></param>
        /// <returns></returns>
        public bool IsCollison(Character character)
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
                BoundingSphere sphere1 = mesh.BoundingSphere;
                sphere1 = sphere1.Transform(Matrix.CreateRotationY(MathHelper.ToRadians(90 * (int)direction)) * Matrix.CreateTranslation(position));
                foreach (ModelMesh mesh2 in character.model.Meshes)
                {
                    BoundingSphere sphere2 = mesh2.BoundingSphere;
                    sphere2 = sphere2.Transform(Matrix.CreateRotationY(MathHelper.ToRadians(90 * (int)character.direction)) * Matrix.CreateTranslation(character.position));
                    if (sphere1.Intersects(sphere2))
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Przemieszczenie obiektu w danym kierunku.
        /// </summary>
        public virtual void Move()
        {
            switch (direction)
            {
                case DirectionType.down:
                    position.Z += (speed / 60.0f);
                    break;
                case DirectionType.right:
                    position.X += (speed / 60.0f);
                    break;
                case DirectionType.up:
                    position.Z -= (speed / 60.0f);
                    break;
                case DirectionType.left:
                    position.X -= (speed / 60.0f);
                    break;
            }
        }
    }
}
